import Vue from 'vue'
import App from './App'
import {
    mapState,
    mapMutations
} from 'vuex'

import store from './store'
import indexHeader from '@/components/indexHeader.vue';
Vue.component('indexHeader',indexHeader)
import bottom from '@/components/bottom.vue';
Vue.component('bottom',bottom)

import ban from '@/components/ban.vue';
Vue.component('ban',ban)
 
Vue.config.productionTip = false;

Vue.prototype.$store = store;
Vue.prototype.host = store.state.host;

Vue.prototype.blzRequest = function(url, data, success, failed, complete) {
    let param = {};
    if (data) {
        param = JSON.parse(JSON.stringify(data));
    }


    if (store.state.hasLogin && !param.phone) {
        param.phone = store.state.blzUserInfo.phone;
        param.token = store.state.blzUserInfo.token;
    }
    let sUrl = url;
    if (url.indexOf('http') > -1) {
        url = url;
    } else {
        url = this.host + url;
    }

};

Vue.prototype.isArray = Array.isArray || function(obj) {
    return obj instanceof Array;
};

Vue.prototype.isEmojiCharacter = function(substring) {

    for (var i = 0; i < substring.length; i++) {
        var hs = substring.charCodeAt(i);
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (substring.length > 1) {
                var ls = substring.charCodeAt(i + 1);
                var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    return true;
                }
            }
        } else if (substring.length > 1) {
            var ls = substring.charCodeAt(i + 1);
            if (ls == 0x20e3) {
                return true;
            }
        } else {
            if (0x2100 <= hs && hs <= 0x27ff) {
                return true;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                return true;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                return true;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                return true;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 ||
                hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b ||
                hs == 0x2b50) {
                return true;
            }
        }
    }
};

//分割数据每3位添加一个逗号考虑小数点的情况
Vue.prototype.transform = function(number) {
    var num = number.toString()
    var numArr = num.split('.')
    var [num, dotNum] = numArr


    var operateNum = num.split('').reverse()
    var result = [],
        len = operateNum.length
    for (var i = 0; i < len; i++) {
        result.push(operateNum[i])
        if (((i + 1) % 3 === 0) && (i !== len - 1)) {
            result.push(',')
        }
    }

    if (dotNum) {
        result.reverse().push('.', ...dotNum)
        return result.join('')
    } else {
        return result.reverse().join('')
    }

};

Vue.prototype.imgReady = (function() {
    var list = [],
        intervalId = null,
        // 用来执行队列
        tick = function() {
            var i = 0;
            for (; i < list.length; i++) {
                list[i].end ? list.splice(i--, 1) : list[i]();
            };
            !list.length && stop();
        },
        // 停止所有定时器队列
        stop = function() {
            clearInterval(intervalId);
            intervalId = null;
        };
    return function(url, ready, load, error) {
        var onready, width, height, newWidth, newHeight,
            img = new Image();
        img.src = url;
        // 如果图片被缓存，则直接返回缓存数据
        if (img.complete) {
            ready.call(img);
            load && load.call(img);
            return;
        };
        width = img.width;
        height = img.height;
        // 加载错误后的事件
        img.onerror = function() {
            error && error.call(img);
            onready.end = true;
            img = img.onload = img.onerror = null;
        };
        // 图片尺寸就绪
        onready = function() {
            newWidth = img.width;
            newHeight = img.height;
            if (newWidth !== width || newHeight !== height || newWidth * newHeight > 1024) {
                // 如果图片已经在其他地方加载可使用面积检测
                ready.call(img);
                onready.end = true;
            };
        };
        onready();
        // 完全加载完毕的事件
        img.onload = function() {
            // onload在定时器时间差范围内可能比onready快
            // 这里进行检查并保证onready优先执行
            !onready.end && onready();
            load && load.call(img);
            // IE gif动画会循环执行onload，置空onload即可
            img = img.onload = img.onerror = null;
        };
        // 加入队列中定期执行
        if (!onready.end) {
            list.push(onready);
            // 无论何时只允许出现一个定时器，减少浏览器性能损耗
            if (intervalId === null) intervalId = setInterval(tick, 40);
        };
    };
})();

//截取H5链接的字符串
Vue.prototype.getQueryString = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.href.split('?')[1].match(reg);
    if (r != null) {
        return decodeURI(r[2]);
    }
    return null;
};

// 过万处理
Vue.prototype.toFixNum = function(num) {
    return num>9999?(num/10000).toFixed(0)+'万':num
};

Vue.prototype.toNumber = function(num) {
    if(num != undefined) {
        let str = num.split(',');
        let n_um = '';
        for(let i = 0; i < str.length;i++) {
            n_um += str[i]
        }
        
        return Number(n_um)
    }
    return ''
};

App.mpType = 'app'

const app = new Vue({
    store,
    ...App
})
app.$mount()
